#pragma once

#include "srect.h"
#include "bitmap.h"
#include "panel.h"
#include "templatearray.h"
class BMPPalletteManager;

class BMPPallette : public Panel
{
private:
	BMPPalletteManager * m_parent;
	BitMaP * m_bmp;
	TemplateArray<int> m_colorOccurance;
	int m_heightPerEntry;
	int m_scroll;
	static const int ENTRY_INSET = 2;
public:
	void countColors(BitMaP * a_bmp)
	{
		m_colorOccurance.setAll(0);
		SPoint start, end(a_bmp->getDimension());
		SPoint pixel(start);
		int dimensionIndex;
		do{
			m_colorOccurance.get(a_bmp->getPixelAt(pixel))++;
			dimensionIndex = 0;
		}while(pixel.iterate(dimensionIndex, start, end));
		setDirty(0);
	}
	void countColors()
	{
		countColors(m_bmp);
	}
	void set(BMPPalletteManager * a_parent, BitMaP * a_bmp, const int & a_heightPerEntry)
	{
		m_parent = a_parent;
		m_bmp = a_bmp;
		m_heightPerEntry = a_heightPerEntry;
		m_colorOccurance.allocateToSize(a_bmp->getColorCount());
		m_scroll = 0;
		countColors(a_bmp);
	}
	BMPPallette():m_parent(0),m_bmp(0),m_heightPerEntry(0),m_scroll(0){}
	void release()
	{
		m_colorOccurance.release();
	}
	virtual void draw(SDL_Surface * a_surface);
	virtual bool handleEvent(const SDL_Event & a_event);
};